package array;

public class RemoveNDuplicates {
    public int removeDuplicates(int[] nums) {
        if (nums.length == 0)
            return 0;
        // 快慢指针，维护nums[0,...slow]为结果子数组
        int slow = 0, fast = 1;
        // 记录一个元素重复的次数
        int count = 1;
        while (fast < nums.length) {
            if (nums[fast] == nums[slow]) {
                count++;
                if (count <= 2) {
                    nums[slow] = nums[fast];
                    slow++;
                }
            } else {
                count = 1;
            }
            fast++;
        }
        return slow + 1;
    }
}
